﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace h2100
{
    public abstract class hVisibleGameObject
    {
        public hSprite[] _sprite;
        public int _nsprite=0;
        private float _x;
        private float _y;
        private float _width;
        private float _height;

        #region Properties

        public float X
        {
            get { return _x; }
            set 
            { 
                _x = value;
                for (int i = 0; i < _nsprite; i++)
                {
                    _sprite[i].X = _x;
                }
            }
        }

        public float Y
        {
            get { return _y; }
            set 
            { 
                _y = value;
                for (int i = 0; i < _nsprite; i++)
                {
                    _sprite[i].Y = _y;
                }
 
            }
        }

        public float Width
        {
            get { return _width; }
            set { _width = value; }
        }

        public float Height
        {
            get { return _height; }
            set { _height = value; }
        }

        private Vector2 _center = new Vector2(0,0);
        public Vector2 Center
        {
            get 
            {
                _center = new Vector2(X + Width / 2, Y + Height / 2);
                return _center;
            }
            set
            { 
                X = value.X - Width / 2;
                Y = value.Y - Height / 2;
                _center = value;
            }
        }

        #endregion

        /// <summary>
        /// Dùng cho trường hợp tạo đối tượng chỉ có một sprite (thường gặp ?!)
        /// </summary>
        /// <param name="content"></param>
        /// <param name="strTextures">Các texture của đối tượng</param> 
        public void create1SpriteObject(ContentManager content, String[] strTextures)
        {
            _nsprite = 1;
            _sprite = new hSprite[_nsprite];            
            Texture2D[] txt = new Texture2D[strTextures.Count()];
            for (int i = 0; i < strTextures.Count(); i++)
            {
                txt[i] = content.Load<Texture2D>(strTextures[i]);               
            }
            _sprite[0] = new hSprite(txt);

            Width = _sprite[0].Width;
            Height = _sprite[0].Height;
            _center = new Vector2(X + Width / 2, Y + Height / 2);
        }

        /// <summary>
        /// Dùng cho trường hợp tạo đối tượng chỉ có một sprite, 1 texture (thường gặp ?!)
        /// </summary>
        /// <param name="content"></param>
        /// <param name="strTextures">Texture của đối tượng</param> 
        public void create1SpriteObject(ContentManager content, String strTextures)
        {
            _nsprite = 1;
            _sprite = new hSprite[_nsprite];
            Texture2D[] txt = new Texture2D[1];
            txt[0] = content.Load<Texture2D>(strTextures);

            _sprite[0] = new hSprite(txt);

            Width = _sprite[0].Width;
            Height = _sprite[0].Height;
            _center = new Vector2(X + Width / 2, Y + Height / 2);
        }

        public void draw(GameTime gtime, SpriteBatch spriteBatch)
        {
            for (int i = 0; i < _nsprite; i++)
            {
                _sprite[i].draw(gtime,spriteBatch);
            }
        }

        public void draw(GameTime gtime, SpriteBatch spriteBatch, int alpha)
        {
            for (int i = 0; i < _nsprite; i++)
            {
                _sprite[i].draw(gtime, spriteBatch, alpha);
            }
        }

        public virtual void update(hParams args, GameTime gameTime)
        {
            for (int i = 0; i < _nsprite; i++)
            {
                _sprite[i].update(args, gameTime);
            }
        }
    }
}
